<template>
	<div class="download" v-show="ifShow">
		<img src="@/assets/img/performance/msgCenter/download.png" @touchend="download()">
	</div>
</template>
<script>
export default {
    props:["showDown","url"],
	data(){
		return{
			ifShow:false,
			once:true,  //防止用户1秒内点击多次,true才能下载
		}
	},
	watch: {
		//弹窗显示/隐藏
		showDown(val) {
			this.ifShow = val;
		},
	},
	methods: {
		//保存
		download:function(){
			//if(this.once){
				console.log('this.once:' + this.once);
				this.once = false;  //防止用户点击多次
				console.log(this.url);
				var timestamp = Date.parse(new Date()); //当前时间戳
				var downLoader = plus.downloader.createDownload(this.url, {
					method: 'GET',
					filename: '_downloads/txz/' + timestamp + '.jpg'
				}, (download, status)=> {
					//下载成功
					if(status == 200){
						console.log('下载成功');
						var fileName = download.filename;
						plus.gallery.save(fileName, ()=> {
							this.once = true;  //防止用户点击多次
							this.$toast({
								duration: 2000,
								message: '保存成功',
							});
						});
					}
					//下载失败
					else{
						console.log('下载失败');
						this.once = true;  //防止用户点击多次
						this.$toast({
							duration: 2000,
							message: '保存失败',
						});
					}
				});
				downLoader.start();
			//}
		},
	},
};
</script>
<style lang="scss" scoped>
	.download{
		position: fixed;
		right: .5rem;
		bottom: .5rem;
		padding: .2rem .26rem;
		border-radius: 6px;
		background-color: #2C2C2C;
		z-index: 20004;
		img{
			width: .4rem;
		}
	}
</style>
